package Shared;

import java.util.Hashtable;

import DispatchSys.RadioSoftware;
import Simulator.Radio;

/**
 * The RadioManager module, see Architecture report for specifications.
 */

public class RadioManager {

	private Hashtable<Integer, Radio> ambsRadio;

	private static final RadioManager singleton = new RadioManager();

	private RadioManager() {
		ambsRadio = new Hashtable<Integer, Radio>();
	}

	/**
	 * @return the unique RadioSoftware instance.
	 */
	public static RadioManager getInstance() {
		return singleton;
	}

	/**
	 * Adds a Radio ambulance to its environments radios.
	 * 
	 * @param radio
	 *            the radio to add
	 */
	public void addAmbulanceRadio(Radio radio) {
		this.ambsRadio.put(radio.getAmbID(), radio);
	}

	/**
	 * Remove a Radio ambulance to its environments radios.
	 * 
	 * @param radio
	 *            the radio to remove
	 */
	public void remAmbulanceRadio(Radio radio) {
		this.ambsRadio.remove(radio.getAmbID());
	}

	/**
	 * Send a message to the Environment world.
	 * 
	 * @param message
	 *            the message to send
	 * @return true if the message was correctly send
	 */
	public boolean sendToWorld(RadioMessage message) {

		Utils.disp("  <-- S " + message);
		return ambsRadio.get(message.getAmbID()).receive(message);

	}

	/**
	 * Send a message to the Software world.
	 * 
	 * @param message
	 *            the message to send
	 * @return true if the message was correctly send
	 */
	public boolean sendToSoftware(RadioMessage message) {
		if (message.getType() != MsgType.POSITION)
			Utils.disp("A -->   " + message);
		return RadioSoftware.getInstance().receive(message);
	}

}
